package com.cn.dao;

import com.cn.BaseDao.BaseDao;
import com.cn.daoIntface.UserAddDaoIntface;
import com.cn.entity.UserAddEntity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserAddDao extends BaseDao implements UserAddDaoIntface {
    @Override
    public Object tableToClass(ResultSet rs) throws Exception {
        return null;
    }
    @Override
    public int add(UserAddEntity entity, String type) throws SQLException, ClassNotFoundException {
        String maxid = this.maxid();
        String sql=null;
        int c=0;
        int rows=0;
        if(type.equals("df")){//添加默认地址
            sql = "INSERT into userAdd (add_id,user_id,users_name,users_add,users_phone,state,users_add_info) VALUES(?,?,?,?,?,0,?)";
            rows= this.executeUpdate(sql, maxid, entity.getUser_id(),entity.getUsers_name(),entity.getUsers_add(),entity.getUsers_phone(),entity.getUsers_add_info());
            System.out.println("新增地址数据成功");
            //将其他的修改为非默认
            sql="UPDATE userAdd set state=1 WHERE user_id=? and NOT add_id=?";
            c=this.executeUpdate(sql,entity.getUser_id(),maxid);
            System.out.println("设置默认地址数据成功");

        }else if (type.equals("nodf")){//添加非默认地址
            System.out.println("新增地址数据成功111");
            sql = "INSERT into userAdd (add_id,user_id,users_name,users_add,users_phone,state,users_add_info) VALUES(?,?,?,?,?,1,?)";
            rows= this.executeUpdate(sql, maxid, entity.getUser_id(),entity.getUsers_name(),entity.getUsers_add(),entity.getUsers_phone(),entity.getUsers_add_info());
            System.out.println("新增地址数据成功");
        }


        return rows;
    }

    @Override
    public int update(UserAddEntity entity, String type) throws SQLException, ClassNotFoundException {
        String sql=null;
        System.out.println(type);
        int rows=0;
        if (type.equals("df")){//默认
            sql="UPDATE userAdd set state=0 WHERE user_id=? and add_id=?";
            rows=this.executeUpdate(sql,entity.getUser_id(),entity.getAdd_id());
            //将同一用户其他的地址设置为非默认
            sql="UPDATE userAdd set state=1 WHERE user_id=? and NOT add_id=?";
            rows=this.executeUpdate(sql,entity.getUser_id(),entity.getAdd_id());
            System.out.println("修改默认");
        }else if (type.equals("ed")){//编辑后不勾选默认
            System.out.println("error1");
            sql="UPDATE userAdd set users_name=? , users_phone=? ,users_add=?,state=1,users_add_info=? where add_id=?";
            rows=this.executeUpdate(sql,entity.getUsers_name(),entity.getUsers_phone(),entity.getUsers_add(),entity.getUsers_add_info(),entity.getAdd_id());
            System.out.println("修改信息");
        }else if (type.equals("eddf")){//编辑勾选默认
            System.out.println("error2");
            sql="UPDATE userAdd set users_name=?,users_phone=?,users_add=?,state=0,users_add_info=? where add_id=?";
            rows=this.executeUpdate(sql,entity.getUsers_name(),entity.getUsers_phone(),entity.getUsers_add(),entity.getUsers_add_info(),entity.getAdd_id());
            sql="UPDATE userAdd set state=1 WHERE user_id=? and NOT add_id=?";
            int c=this.executeUpdate(sql,entity.getUser_id(),entity.getAdd_id());
            //修改其他的为默认
            System.out.println("修改信息");
        }else if (type.equals("del")){
            sql="DELETE FROM userAdd WHERE add_id=?";
            rows=this.executeUpdate(sql,entity.getAdd_id());
            System.out.println("删除信息");
        }else {
            System.out.println("error");
        }
        System.out.print(rows);
        return rows;
    }

    @Override
    public List<UserAddEntity> queryAll(Integer currentPageNo, Integer pageSize) throws SQLException, ClassNotFoundException {
        return null;
    }



    @Override
    public List<UserAddEntity> queryAll(String id) {
        String sql=null;
        List<UserAddEntity> list=null;
        try {
                sql="SELECT * FROM userAdd WHERE user_id = ? ";
                list=(List<UserAddEntity>) this.executeQuery(sql,UserAddEntity.class,id);
        }catch (Exception e){
            System.out.print("查询错误");
        }
        return list;
    }

    public UserAddEntity queryOne(String id) {
        String sql=null;
        UserAddEntity entity=null;
        try {
            sql="SELECT * FROM userAdd WHERE add_id = ? ";
            entity=(UserAddEntity) this.executeQueryOne(sql,UserAddEntity.class,id);
        }catch (Exception e){
            System.out.print("查询错误");
        }
        return entity;
    }
    @Override
    public List<UserAddEntity> queryAll() {
        return null;
    }

    /**
     *
     * @return  返回最大的id
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    @Override
    public String maxid() throws SQLException, ClassNotFoundException {
        return this.getStringNumber(1, "AD", "0000", "add_id", "userAdd");
    }
}
